layui.use(['table', 'jquery', 'laydate'], function () {
    const table = layui.table,
        $ = layui.jquery;

    //显示日期
    showTime('#startDate');
    showTime('#endDate');

    //默认加载时执行
    findAll();
    findCodeByType('gender', $("#gender"));

    function findAll() {

        let code = $('#code').val();
        let username = $('#username').val();
        let gender = $("#gender option:selected").val();
        let startDate = $('#startDate').val();
        let endDate = $('#endDate').val();
        let params = {
            code: code,
            username: username,
            gender: gender,
        };
        if (startDate !== undefined && startDate != null && startDate !== '') {
            params['startDate'] = startDate
        }
        if (endDate !== undefined && endDate != null && endDate !== '') {
            params['endDate'] = endDate
        }

        let cols = [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', width: 80, title: 'ID', sort: true, fixed: 'left'}
            , {field: 'code', width: 100, title: '用户编号', sort: true}
            , {field: 'stuId', width: 100, title: '学号', sort: true}
            , {field: 'username', width: 140, title: '用户名'}
            , {
                field: 'avatar', width: 120, title: '头像', templet: function (e) {
                    //判断图片是否为空
                    e.avatar === null || e.avatar === undefined ? e.avatar = '/upload/img_null.png' : e.avatar;
                    return '<img src="' + e.avatar + '" width="80px" height="30px">'
                }
            }
            , {
                field: 'gender', width: 80, title: '性别', templet: e => {
                    return getGenderStatus(e.gender);
                }
            }
            , {
                field: 'dataEnable', width: 120, title: '状态', templet: function (e) {
                    return showDataEnable(e);
                }
            }
            , {field: 'phone', width: 160, title: '电话'}
            , {field: 'birthday', title: '生日', width: 160}
            , {
                field: 'degree', title: '学历', templet: e => {
                    return getDegreeStatus(e.degree);
                }
            }
            , {field: 'joinTime', title: '入学时间', width: 160}
            , {field: 'joinFrequency', title: '入学班次', width: 160}
            , {field: 'createTime', title: '创建时间', width: 160}
            , {field: 'updateTime', title: '更新时间', width: 160}
            , {fixed: 'right', width: 80, align: 'center', toolbar: '#operating'}
        ]];

        //发送请求
        table.render({
            elem: '#dataList'
            , url: AUTH_SERVER_URL + "/user/findAll"
            , where: params
            , method: 'get'
            , headers: HEADERS
            , request: REQUEST
            , response: RESPONSE
            , cellMinWidth: 80
            , height: 720
            , toolbar: '#toolbar'
            , skin: 'line'
            , even: true
            , page: true
            , limits: [5, 10, 20, 30, 50]
            , limit: 20
            , title: '用户数据表'
            , cols: cols
        });
    }

    //监听头工具栏事件
    table.on('toolbar(dataList)', function (obj) {
        const checkStatus = table.checkStatus(obj.config.id)
            , data = checkStatus.data; //获取选中的数据
        switch (obj.event) {
            case 'enable':
                if (data.length === 0) {
                    layer.msg('请选择一行');
                } else if (data.length > 1) {
                    layer.msg('只能同时编辑一个');
                } else {
                    dataEnable('yes', checkStatus.data[0].id);
                }
                break;
            case 'disabled':
                if (data.length === 0) {
                    layer.msg('请选择一行');
                } else if (data.length > 1) {
                    layer.msg('只能同时编辑一个');
                } else {
                    dataEnable('no', checkStatus.data[0].id);
                }
                break;
        }
    });

    //监听行工具事件
    table.on('tool(dataList)', function (obj) {
        const data = obj.data //获得当前行数据
            , layEvent = obj.event;
        if (layEvent === 'edit') {
            updateUser(data.id);
        }
    });


    /**
     * 修改
     * @param id
     */
    function updateUser(id) {
        sessionStorage.setItem("userId", id);
        commonLayerOpen('用户详情', 'userDetails.html');
    }

    /**
     * 查询
     */
    $('#query').on('click', function () {
        findAll()
    });

    /**
     * 重置
     */
    $('#reset').on('click', function () {
        $('#code').val('');
        $('#username').val('');
        $('#gender').val('');
        $('#startDate').val('');
        $('#endDate').val('');
    });

    /**
     * 启用与禁用
     * @param type
     * @param id
     */
    function dataEnable(type, id) {
        let data = {
            id: id,
            dataEnable: type
        };
        let result = commonAjax(AUTH_SERVER_URL+'/user/update', JSON.stringify(data), 'post');
        if (result.code === 200) {
            layerMsgOperatingSuccess();
        } else {
            layerMsgOperatingFail();
        }
        findAll();
    }

});